package ModelLayer; //makes this class a part of the ModelLayer package
import java.util.*;

/**
 * 
 * <code>AddressBook</code> class stores a collection of all of the friends and manages the data flow between <code>Person</code> class and the AddressBook Controller.
 * 
 * @author (dmaj0913 - group 2) 
 * @version (2013.11.08)
 */
public class AddressBook
{
    private ArrayList<Person> people;

    /**
     * Constructor creates new instance of <code>Person</code> class.
     */
    public AddressBook()
    {
        people = new ArrayList<Person>();
    }
    
    /**
     * Adds a unique friend to the collection of people in the System.
     */
    public void addPerson(Person newperson)
    {
        Iterator<Person> it = people.iterator();
        boolean exists = false;
        while(it.hasNext())
        {
            Person test = it.next();
            if(newperson.getId() == test.getId()) exists = true;
        }
        if(!exists) people.add(newperson);
    }
    
    /**
     * Returns all friends in the AddressBook
     * @return an ArrayList of type person with all elements of Addressbook collection.
     */
    public ArrayList<Person> getPersons()
    {
        return people;
    }
    
    /**
     * Finds a friend by a specific ID and returns him.
     * @param id stores the desired id of the person that the system is searching for.
     * @return The desired friend or <code>null</code> if he is not found in the System.
     */ 
    public Person getPerson(long id)
    {
        Iterator<Person> it = people.iterator();
        while(it.hasNext())
        {
            Person pers = it.next();
            if(pers.getId() == id) return pers;
        }
        return null;
    }
    
    /**
     * Deletes a specific friend by finding him in the collection.
     * @param todelete stores a friend of type Person who has to be deleted from the AddressBook.
     */
    public void deletePerson(Person todelete)
    {
        Iterator<Person> it = people.iterator();
        while(it.hasNext())
        {
            Person pers = it.next();
            if(todelete.getId() == pers.getId()) it.remove(); 
        }
    }
    
    /**
     * Finds a friend in the collection and edits all his fields(name, address, post code, city, phone number).
     * @param toupdate stores a friend of type Person that has to be updated.
     * @param name stores a character string of the new name of the friend.
     * @param address stores a character string of the new address of the friend.
     * @param postal stores a character string of the new Postal Code of the friend.
     * @param city stores a character string of the new city of the friend.
     * @param tel stores a character string of the new phone number of the friend.
     */
    public void updatePerson(Person toupdate, String name, String address,String postal, String city, String tel)
    {
        Iterator<Person> it = people.iterator();
        while(it.hasNext())
        {
            Person pers = it.next();
            if(toupdate.getId() == pers.getId()) 
            {
                pers.setName(name);
                pers.setAddress(address);
                pers.setPostalCode(postal);
                pers.setCity(city);
                pers.setPhoneNumber(tel);
            }
        }
    }
}
